package com.huike.service.impl.sysImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.constans.SysConstantsMessage;
import com.huike.mapper.sys.RoleMapper;
import com.huike.result.PageResult;
import com.huike.Role.dtos.RoleDto;
import com.huike.Role.dtos.RolePageDTO;
import com.huike.Role.pojos.Role;
import com.huike.service.sys.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    //条件分页查询角色信息
    @Override
    public PageResult list(RolePageDTO rolePageDTO) {
        //判断非空为空就初始化
        rolePageDTO.initialize();
        IPage page = new Page(rolePageDTO.getPage(), rolePageDTO.getPageSize());
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.like(rolePageDTO.getRoleName() != null && !"".equals(rolePageDTO.getRoleName()), Role::getRoleName, rolePageDTO.getRoleName());
        lqw.eq(rolePageDTO.getStatus() != null, Role::getStatus, rolePageDTO.getStatus());

        roleMapper.selectPage(page, lqw);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());
        return pageResult;
    }

    //添加角色
    @Override
    public void saveRole(RoleDto roleDto) {
        if (roleDto == null)
            throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        //校验参数
        boolean flag = ifParamNotRoleDtoNUll(roleDto);
        if (!flag) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        if (roleDto.getStatus() == null) roleDto.setStatus(1);
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        //角色验证唯一
       Integer id = roleMapper.getRoleName(role);
       if (id != null)throw new RuntimeException(SysConstantsMessage.ROLE_NAME_IS_TWO);
        role.setCreateTime(LocalDateTime.now());
        role.setCreateUser(1L);
        role.setDeptId(1);
        role.setMenuId(1);
        role.setScopeId(1);
        roleMapper.save(role);

    }


    private boolean ifParamNotRoleDtoNUll(RoleDto roleDto) {
        boolean flag = true;
        if (roleDto.getRoleName() == null) flag = false;
        if (roleDto.getPermission() == null) flag = false;
        if (roleDto.getStatus() == null) flag = false;
        return flag;
    }

    //修改角色
    @Override
    public void updateRole(RoleDto roleDto) {
        if (roleDto == null) throw new RuntimeException("參數不存在");
        //校验参数
        boolean flag = ifParamNotRoleDtoNUll(roleDto);
        if (!flag) throw new RuntimeException("参数为NULL");
        if (roleDto.getStatus() == null) roleDto.setStatus(1);
        Role role = new Role();
        role.setCreateTime(LocalDateTime.now());
        role.setCreateUser(1L);
        role.setDeptId(1);
        role.setMenuId(1);
        role.setScopeId(1);
        BeanUtils.copyProperties(roleDto, role);
        roleMapper.updateRole(role);

    }

    @Override
    public Role findById(Integer id) {
        if (id == null)throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        return roleMapper.findById(id);
    }

    //根据id删除数据
    @Override
    public void deleteById(Integer id) {
        if (id == null)throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        roleMapper.deleteRole(id);
    }
}
